#!/bin/bash
# Zaitan Module - Nagios Install
# Zaitan Team <http://code.google.com/p/zaitan/>
# Script Tirado desse link http://www.vivaolinux.com.br/artigos/verArtigo.php?codigo=6788&pagina=1


RMOD_ENGINE='1.0'
RMOD_CATEGORY='Software'
RMOD_SUBCATEGORY='Servers'
RMOD_NAME='Nagios'
RMOD_AUTHOR='Zaitan Team'
RMOD_VERSION='2.9'
RMOD_DESCRIPTION='Installs Nagios'



# Install Nagios

echo Running $RMOD_NAME...
#/usr/lib/zaitan/modulo/mod-checkInstall

cd /usr/share/zaitan

apt-get update
apt-get -y install openssl
apt-get -y install libssl-dev 
apt-get -y install mcrypt 
apt-get -y install build-essential 
apt-get -y install nmap 
apt-get -y install xinetd 
apt-get -y install apache2 
apt-get -y install libjpeg62 
apt-get -y install libjpeg-dev 
apt-get -y install libpng12-0 
apt-get -y install libpng12-dev 
apt-get -y install libgd2-xpm 
apt-get -y install libgd2-xpm-dev 
apt-get -y install fontconfig
apt-get -y install sudo


useradd -m nagios
groupadd nagios
usermod -G nagios nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios
groupadd nagcmd
usermod -G nagios nagcmd



wget -c http://ufpr.dl.sourceforge.net/sourceforge/nagios/nsca-2.7.2.tar.gz





tar xzvf nsca-2.7.2.tar.gz



## Compilando o nagios
wget -c http://ufpr.dl.sourceforge.net/sourceforge/nagios/nagios-2.9.tar.gz
tar xzvf nagios-2.9.tar.gz
cd nagios-2.9

./configure --prefix=/usr/local/nagios --with-cgiurl=/nagios/cgi-bin --with-htmurl=/nagios/ --with-nagios-user=nagios --with-nagios-grp=nagios --with-gd-lib=/usr/lib --with-gd-inc=/usr/lib


make all
make install
make install-init
make install-commandmode
make install-config
 


cd ..
wget -c http://ufpr.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.9.tar.gz
tar xzvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure
make all
make install

cd /usr/local/nagios/etc
mkdir original
cp *.cfg-sample original

for i in *cfg-sample; do
mv $i `echo $i|sed -e s/cfg-sample/cfg/`
done 

#mkdir /usr/local/nagios/var/rw 
chown -R nagios.nagios /usr/local/nagios/var/rw
chmod u+rwx /usr/local/nagios/var/rw
chmod g+rwx /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 

cd /usr/share/zaitan
wget -c http://ufpr.dl.sourceforge.net/sourceforge/nagios/nrpe-2.9.tar.gz
tar xzvf nrpe-2.9.tar.gz
cd nrpe-2.9
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd 

echo 'nagios          ALL=NOPASSWD:ALL' | tee -a /etc/sudoers
echo 'nrpe            5666/tcp # NRPE'| tee -a /etc/services

/etc/init.d/xinetd restart



## Instalando o nsca
cd ..
cd nsca-2.7.2
./configure
make
make install 


cd ..
wget -c http://packzaitan.googlecode.com/files/nagios_skyns.tar.gz
tar xzvf nagios_skyns.tar.gz
mv /usr/local/nagios/share /usr/local/nagios/share.old
mv share /usr/local/nagios/
mkdir /usr/local/nagios/etc/services
mkdir /usr/local/nagios/etc/hosts
mkdir /usr/local/nagios/etc/backup
mkdir /usr/local/nagios/etc/backup/hosts
mkdir /usr/local/nagios/etc/backup/services
chown -R nagios.nagios /usr/local/nagios/*

## Configurando o Apache2
echo 'ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin/
<Directory "/usr/local/nagios/sbin/">
   AllowOverride AuthConfig
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>
Alias /nagios /usr/local/nagios/share/
<Directory "/usr/local/nagios/share">
   Options None
   #AllowOverride AuthConfig
   #Order allow,deny
   #Allow from all
   #AuthName "Nagios Access"
   #AuthType Basic
   #AuthUserFile /usr/local/nagios/etc/htpasswd.users
   #require valid-user
</Directory>' > nagios

sed -i 's/^use_authentication=.*/use_authentication=0/' /usr/local/nagios/etc/cgi.cfg

ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
cp nagios /etc/apache2/sites-enabled/

/etc/init.d/apache2 restart
/etc/init.d/nagios restart


echo $RMOD_NAME Finibashed...

##Logs
echo $RMOD_NAME-$RMOD_VERSION | tee -a /usr/share/zaitan/logs
/usr/lib/zaitan/modulo/mod-logs
##

exit 0
